﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tecnopez.Globals;
/// <summary>
/// Grilla de ABM con botoncito para ver
/// </summary>
public partial class Controls_TecnopezDataGRIDABMView : System.Web.UI.UserControl
{
    public delegate void viewGridClic(int indice);
    public delegate void viewGridRowDataBound(object sender, GridViewRowEventArgs e);

    private viewGridClic myViewGridClic;
    private viewGridRowDataBound myViewGridRowDataBound;
    private StateBag MyViewState;
    private String pageDataViewOrdenKey;
    private String pageDataViewOrdenDireccionKey;
    private Boolean banNoMostrarBotonVer = false;
    /// <summary>
    /// Evento que se genera al presinar el boton view de la grilla
    /// </summary>
    public event viewGridClic ViewGridClic
    {
        add
        {
            myViewGridClic += value;
        }
        remove
        {
            myViewGridClic -= value;
        }
    }


    /// <summary>
    /// Evento que se genera al cuando enlazan los datos de la grilla
    /// </summary>
    public event viewGridRowDataBound ViewGridRowDataBound
    {
        add
        {
            myViewGridRowDataBound += value;
        }
        remove
        {
            myViewGridRowDataBound -= value;
        }
    }

    public String PageDataViewOrdenKey
    {
        get
        {
            return MyViewState[pageDataViewOrdenKey].ToString(); ;
        }
        set
        {
            MyViewState[pageDataViewOrdenKey] = value;
        }
    }

    public String PageDataViewOrdenDireccionKey
    {
        get
        {
            return MyViewState[pageDataViewOrdenDireccionKey].ToString();
        }
        set
        {
            MyViewState[pageDataViewOrdenDireccionKey] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// Funcion que configura la grilla
    /// </summary>
    /// <typeparam name="T">tipo de dato entidad</typeparam>
    /// <param name="cargarGrilla">funcion que carga la grilla en la pagina cliente del control</param>
    /// <param name="eventWhenClicView">funcion que se llamara cuando el usuario haga clic en ver</param>
    /// <param name="compactView">si desea que la grilla se muestre o no en forma compacta.</param>
    public void setupGrid<T>(GrdViewInitializator<T>.cargarGrillaHandler cargarGrilla, viewGridClic eventWhenClicView, viewGridRowDataBound eventWhenRowDataBound, bool compactView, StateBag pageViewState) where T : new()
    {
        MyViewState = pageViewState;

        if(MyViewState[pageDataViewOrdenKey] ==null)
        {
            MyViewState[pageDataViewOrdenKey] = "orden";
        }
        if (MyViewState[pageDataViewOrdenDireccionKey]==null)
        {
        MyViewState[pageDataViewOrdenDireccionKey] = "ordenDireccion";
        }

        this.ViewGridClic += eventWhenClicView;
        this.ViewGridRowDataBound += eventWhenRowDataBound;

        GrdViewInitializator<T> confG = new GrdViewInitializator<T>(pageViewState,this.PageDataViewOrdenKey,this.PageDataViewOrdenDireccionKey, true, cargarGrilla);
        ButtonField colB = new ButtonField();
        colB.ButtonType = ButtonType.Image;
        colB.ImageUrl = Functions.getHost(this.Page) + "Styles/Icons/lupa.png";
        //colB.Text = "ver";
        colB.HeaderText = "Edición";
        colB.CommandName = "ver";
        colB.HeaderStyle.Width = 5;
        colB.ItemStyle.HorizontalAlign = HorizontalAlign.Center;

        ButtonField[] d = new ButtonField[1];
        d[0] = colB;

        confG.gridViewSetup(this.grdDatos, compactView, d);
    } 
    
    /// <summary>
    /// Funcion que configura la grilla
    /// </summary>
    /// <typeparam name="T">tipo de dato entidad</typeparam>
    /// <param name="cargarGrilla">funcion que carga la grilla en la pagina cliente del control</param>
    /// <param name="eventWhenClicView">funcion que se llamara cuando el usuario haga clic en ver</param>
    /// <param name="compactView">si desea que la grilla se muestre o no en forma compacta.</param>
    /// <param name="extraButtons">Coleccion de botones a agregar despues del boton tipico de abm de ver. para usos futuros.</param>
    public void setupGrid<T>(GrdViewInitializator<T>.cargarGrillaHandler cargarGrilla, viewGridClic eventWhenClicView, viewGridRowDataBound eventWhenRowDataBound, bool compactView, DataControlField[] extraButtons, StateBag pageViewState) where T : new()
    {
        setupGrid<T>(cargarGrilla, eventWhenClicView, eventWhenRowDataBound, compactView, extraButtons, pageViewState, false);
    }


    /// <summary>
    /// Funcion que configura la grilla
    /// </summary>
    /// <typeparam name="T">tipo de dato entidad</typeparam>
    /// <param name="cargarGrilla">funcion que carga la grilla en la pagina cliente del control</param>
    /// <param name="eventWhenClicView">funcion que se llamara cuando el usuario haga clic en ver</param>
    /// <param name="compactView">si desea que la grilla se muestre o no en forma compacta.</param>
    /// <param name="extraButtons">Coleccion de botones a agregar despues del boton tipico de abm de ver. para usos futuros.</param>
    public void setupGrid<T>(GrdViewInitializator<T>.cargarGrillaHandler cargarGrilla, viewGridClic eventWhenClicView, viewGridRowDataBound eventWhenRowDataBound, bool compactView, DataControlField[] extraButtons, StateBag pageViewState, bool noMostrarBotonVer) where T : new()
    {
        MyViewState = pageViewState;
        banNoMostrarBotonVer = noMostrarBotonVer;
        if (MyViewState[pageDataViewOrdenKey] == null)
        {
            MyViewState[pageDataViewOrdenKey] = "orden";
        }
        if (MyViewState[pageDataViewOrdenDireccionKey] == null)
        {
            MyViewState[pageDataViewOrdenDireccionKey] = "ordenDireccion";
        }

        this.ViewGridClic += eventWhenClicView;
        this.ViewGridRowDataBound += eventWhenRowDataBound;
        GrdViewInitializator<T> confG = new GrdViewInitializator<T>(pageViewState, this.PageDataViewOrdenKey, this.PageDataViewOrdenDireccionKey, true, cargarGrilla);
        if (!noMostrarBotonVer)
        {
            ButtonField colB = new ButtonField();
            colB.ButtonType = ButtonType.Image;
            colB.ImageUrl = Functions.getHost(this.Page) + "Styles/Icons/lupa.png";
            colB.HeaderText = "Edición";
            colB.CommandName = "ver";
            colB.HeaderStyle.Width = 5;
            colB.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            DataControlField[] d = new DataControlField[1 + extraButtons.Length];
            extraButtons.CopyTo(d, 0);
            d[d.Length - 1] = colB;
            confG.gridViewSetup(this.grdDatos, compactView, d);
        }
        else
        {
            confG.gridViewSetup(this.grdDatos, compactView, extraButtons);
        }
    }

    public System.Web.UI.WebControls.GridView grdDatosAbmView
    {
        get
        {
            return this.grdDatos;
        }
        set
        {
            this.grdDatos = value;
        }
    }

    protected void grdDatos_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (banNoMostrarBotonVer)
        {
            if (myViewGridClic != null)
            {
                int o = int.Parse(e.CommandArgument.ToString());
                myViewGridClic(o);
            }
            else
            {
                throw new Tecnopez.Globals.TecnoPezWEBException("Debe setear el evento en la grilla de abm para poder utilizar el boton");
            }
        }
        else
        {
            if (e.CommandName == "ver")
            {
                int o = int.Parse(e.CommandArgument.ToString());
                if (myViewGridClic != null)
                {
                    myViewGridClic(o);
                }
                else
                {
                    throw new Tecnopez.Globals.TecnoPezWEBException("Debe setear el evento en la grilla de abm para poder utilizar el link de ver");
                }
            }
        }
    }

    protected void grdDatos_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (banNoMostrarBotonVer) //fix para cuando no se usa como abm
            return;

        int IndiceVer = 0;
        for (int celda = 0; celda < grdDatos.Columns.Count; celda++)
        {
            if (grdDatos.Columns[celda].HeaderText == "Edición")
            {
                IndiceVer = celda;
            }
        }

        if (e.Row.DataItem != null)
        {
            ImageButton imgVer = (ImageButton)e.Row.Cells[IndiceVer].Controls[0];
            imgVer.Width = 20;
            imgVer.ID = "imagen" + e.Row.RowIndex;
        }

        if (myViewGridRowDataBound != null)
        {
            myViewGridRowDataBound(sender,e);
        }
    }

    protected void grdDatos_Init(object sender, EventArgs e)
    {
        pageDataViewOrdenKey = "pageDataViewOrdenKey" + this.grdDatos.ClientID;
        pageDataViewOrdenDireccionKey = "pageDataViewOrdenDireccionKey" + this.grdDatos.ClientID;
    }
}
